/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.boot.web;

import com.iwindplus.boot.util.JacksonUtil;
import com.iwindplus.boot.web.domain.property.JacksonProperty;
import com.iwindplus.boot.web.sensitive.SensitiveAnnotationIntrospect;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

/**
 * jackson相关配置.
 *
 * @author zengdegui
 * @since 2020/11/8
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(JacksonProperty.class)
@ConditionalOnProperty(prefix = "jackson", name = "enabled", havingValue = "true", matchIfMissing = true)
public class JacksonConfiguration {

    @Resource
    private JacksonProperty property;

    @Resource
    private JacksonProperties jacksonProperties;

    /**
     * 创建 Jackson2ObjectMapperBuilder.
     *
     * @return Jackson2ObjectMapperBuilder
     */
    @Bean
    @Primary
    public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
        final Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = JacksonUtil.jackson2ObjectMapperBuilder(this.jacksonProperties);
        jackson2ObjectMapperBuilder.annotationIntrospector(new SensitiveAnnotationIntrospect(this.property.getSensitive().getEnabled()));
        log.info("Jackson2ObjectMapperBuilder={}", jackson2ObjectMapperBuilder);
        return jackson2ObjectMapperBuilder;
    }

    /**
     * 创建 MappingJackson2HttpMessageConverter.
     *
     * @param jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder
     * @return MappingJackson2HttpMessageConverter
     */
    @ConditionalOnMissingBean
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
        final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(
            jackson2ObjectMapperBuilder.build());
        log.info("MappingJackson2HttpMessageConverter={}", mappingJackson2HttpMessageConverter);
        return mappingJackson2HttpMessageConverter;
    }

    /**
     * 创建HttpMessageConverters.
     *
     * @param mappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter
     * @return HttpMessageConverters
     */
    @ConditionalOnMissingBean
    @Bean
    public HttpMessageConverters jacksonHttpMessageConverters(MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter) {
        final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(mappingJackson2HttpMessageConverter);
        log.info("HttpMessageConverters={}", httpMessageConverters);
        return httpMessageConverters;
    }
}
